class SchoolsController < ApplicationController
  layout "general"
  before_filter :login_required, :except => [:index, :show]
=begin
  def homepage
    @broadcasts = Broadcast.find(:all, :limit => 15,
                                 :conditions => "broadcasts.site=2",
                                 :order => "broadcasts.created_at desc",
                                 :joins => "left join users on broadcasts.from = users.id",
                                 :select => "broadcasts.content, broadcasts.from, broadcasts.created_at, users.login, users.id")
    
    if flash[:notice].blank? && logged_in? && !Profile.find(:first, :conditions => ["user_id = ?",self.current_user.id])
      flash[:notice] = "填写个人资料，参与放假多背一公斤活动 ;) <a href=\"/profile\">填写个人资料</a>"
    end
  end
=end
  # GET /schools
  def index
    @schools = School.find(:all, :conditions => "province_id is not null and province_id <> 1", 
                                 :order => "province_id asc, updated_at desc",
                                 :include => :province)
    @provinces = @schools.collect { |school| school.province }.uniq
  end
  
  # GET /schools/1
  def show
    @school = School.find(params[:id])
    #@comments = @school.comments.find(:all, :order => "created_at asc")
    #@comment = SchoolComment.new
    respond_to do |format|
      format.html
    end
  end
  
  # GET /schools/new
  def new
    @school = School.new(:area_id => params[:area])
  end
  
  # POST /schools
  def create
    @school = School.new(params[:school])
    @school.user = self.current_user
    # get province_id
    @school.province = @school.area.root unless @school.area.blank?
    @school.category = 2 # aixin1kg
    @school.save!
    
    bc = Broadcast.create(:site => 2, :from=>0, :to=>0, :content=>"<a href=\"#{user_path(self.current_user)}\">#{self.current_user.login}</a> 提供了一所学校信息")
    
    respond_to do |format|
      format.html { 
        flash[:notice] = "您提交的学校信息已经成功保存，我们会与学校取得联系"
        redirect_to :controller => 'biz', :action => 'join'
      }
    end
  end
  
  # GET /schools/1;edit
  def edit
    @school = School.find(params[:id])
  end
  
  # PUT /schools/1
  def update
    @school = School.find(params[:id], :include => :area)
    # get province_id
    params[:school][:province_id] = Area.find(params[:school][:area_id]).root unless @school.area.blank?
    
    @school.update_attributes!(params[:school])
    respond_to do |format|
      format.html { redirect_to school_path(@school)}
    end
  end
  
  # DELETE /schools/1
  def destroy
    @school = School.find(params[:id])
    @school.destroy
    respond_to do |format|
      format.html { redirect_to area_path(@school.area_id) }
    end
  end
  
  # PUT /schools/1;validate
  def validate
    @school = School.find(params[:id], :include => :area)
    @school.update_attributes!(:has_validate => params[:v])
    respond_to do |format|
      format.html { redirect_to school_path(@school.area, @school) }
    end
  end
  
  protected

end
